Ethereum のトランザクション作成時に必要な nonce を得る
ローカルで Ethereum のトランザクションに署名する際には、nonce を指定する必要があります。
このときに必要な nonce を取得する方法をメモしておきます。
nonce とは
Ethereum のトランザクションにおける nonce は、ある Ethereum のアドレス(アカウント)から実行されたトランザクションの総数です。
nonce は各トランザクションがそれぞれ一度のみ実行されることを保証するために必要な値です。
0 から 1 つずつインクリメントされていきます。
Ethereum アドレスの現在の nonce を取得する
Ethereum のトランザクションを作成するためには、トランザクションを作成したいアカウントが過去に何回トランザクションを実行していたか(トランザクションの総数==現在の nonce)を知る必要があります。
Ethereum アドレスの現在の nonce は、web3.js を使用する場合は web3.eth.getTransactionCount で取得することができます。
web3.eth.getTransactionCount では、過去のトランザクション数が0回であれば0を、3回であれば3を返します。
サンプルコード
code:example.js
web3.eth.getTransactionCount(address, (error, count) => {
console.log(count);
});
#動作デモ
https://piyolab.github.io/playground/ethereum/getTransactionCount/index.html
参考
https://github.com/ethereum/wiki/wiki/White-Paper
https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethgettransactioncount
web3.eth.getTransactionCount
web3.js web3.eth.getTransactionCount
web3.js@0.2x.x web3.eth.getTransactionCount
関連
http://blog.playground.io/entry/2018/05/25/103924
#howto